package com.yibo.user.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.yibo.support.pojo.IResult;
import com.yibo.support.pojo.Result;
import com.yibo.db.user.entity.UserInfo;
import com.yibo.user.object.YBUser;
import com.yibo.user.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import static com.yibo.support.constant.ApiUrlConstant.PRIVATE_URL;
import static com.yibo.support.helper.ResultHelper.result;

@RestController
@RequestMapping(PRIVATE_URL+"/user")
public class PrivateUserController {

    @Resource
    private UserService userService;


    @GetMapping("/test")
    public IResult<UserInfo> userInfoIResult(){
        return new Result<>(1,"",new UserInfo().setUserId("fsdlkfj").setPhone("fdsjkfhsjkfh"));
    }


    @ApiOperation("查询当前登陆用户的信息")
    @GetMapping("/query/login/user")
    @HystrixCommand(fallbackMethod = "queryLoginUserError") //注意此处使用hystrixCommand后，RequestContextHolder.getRequestAttributes()将不起作用
    //需要更改hystrix隔离策略： hystrix.command.default.execution.isolation.strategy: SEMAPHORE
    public IResult<YBUser>  queryLoginUser(){
        YBUser user = userService.queryLoginUser();
        return result("查询成功",user);
    }

    public IResult<YBUser> queryLoginUserError(){
        return new Result<>();
    }


    @ApiOperation("退出用户")
    @GetMapping("/login/out")
    public IResult<Integer>  loginOut(){
        Integer result = userService.loginOut();
        return result("退出成功",result);
    }

}
